{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 12.7 \u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u6c60\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u521b\u5efa\u4e00\u4e2a\u5de5\u4f5c\u8005\u7ebf\u7a0b\u6c60\uff0c\u7528\u6765\u54cd\u5e94\u5ba2\u6237\u7aef\u8bf7\u6c42\u6216\u6267\u884c\u5176\u4ed6\u7684\u5de5\u4f5c\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "concurrent.futures \u51fd\u6570\u5e93\u6709\u4e00\u4e2a ThreadPoolExecutor \u7c7b\u53ef\u4ee5\u88ab\u7528\u6765\u5b8c\u6210\u8fd9\u4e2a\u4efb\u52a1\u3002\n\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684TCP\u670d\u52a1\u5668\uff0c\u4f7f\u7528\u4e86\u4e00\u4e2a\u7ebf\u7a0b\u6c60\u6765\u54cd\u5e94\u5ba2\u6237\u7aef\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from socket import AF_INET, SOCK_STREAM, socket\nfrom concurrent.futures import ThreadPoolExecutor\n\ndef echo_client(sock, client_addr):\n    '''\n    Handle a client connection\n    '''\n    print('Got connection from', client_addr)\n    while True:\n        msg = sock.recv(65536)\n        if not msg:\n            break\n        sock.sendall(msg)\n    print('Client closed connection')\n    sock.close()\n\ndef echo_server(addr):\n    pool = ThreadPoolExecutor(128)\n    sock = socket(AF_INET, SOCK_STREAM)\n    sock.bind(addr)\n    sock.listen(5)\n    while True:\n        client_sock, client_addr = sock.accept()\n        pool.submit(echo_client, client_sock, client_addr)\n\necho_server(('',15000))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u60f3\u624b\u52a8\u521b\u5efa\u4f60\u81ea\u5df1\u7684\u7ebf\u7a0b\u6c60\uff0c\n\u901a\u5e38\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2aQueue\u6765\u8f7b\u677e\u5b9e\u73b0\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7a0d\u5fae\u4e0d\u540c\u4f46\u662f\u624b\u52a8\u5b9e\u73b0\u7684\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from socket import socket, AF_INET, SOCK_STREAM\nfrom threading import Thread\nfrom queue import Queue\n\ndef echo_client(q):\n    '''\n    Handle a client connection\n    '''\n    sock, client_addr = q.get()\n    print('Got connection from', client_addr)\n    while True:\n        msg = sock.recv(65536)\n        if not msg:\n            break\n        sock.sendall(msg)\n    print('Client closed connection')\n\n    sock.close()\n\ndef echo_server(addr, nworkers):\n    # Launch the client workers\n    q = Queue()\n    for n in range(nworkers):\n        t = Thread(target=echo_client, args=(q,))\n        t.daemon = True\n        t.start()\n\n    # Run the server\n    sock = socket(AF_INET, SOCK_STREAM)\n    sock.bind(addr)\n    sock.listen(5)\n    while True:\n        client_sock, client_addr = sock.accept()\n        q.put((client_sock, client_addr))\n\necho_server(('',15000), 128)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528 ThreadPoolExecutor \u76f8\u5bf9\u4e8e\u624b\u52a8\u5b9e\u73b0\u7684\u4e00\u4e2a\u597d\u5904\u5728\u4e8e\u5b83\u4f7f\u5f97\n\u4efb\u52a1\u63d0\u4ea4\u8005\u66f4\u65b9\u4fbf\u7684\u4ece\u88ab\u8c03\u7528\u51fd\u6570\u4e2d\u83b7\u53d6\u8fd4\u56de\u503c\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u80fd\u4f1a\u50cf\u4e0b\u9762\u8fd9\u6837\u5199\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from concurrent.futures import ThreadPoolExecutor\nimport urllib.request\n\ndef fetch_url(url):\n    u = urllib.request.urlopen(url)\n    data = u.read()\n    return data\n\npool = ThreadPoolExecutor(10)\n# Submit work to the pool\na = pool.submit(fetch_url, 'http://www.python.org')\nb = pool.submit(fetch_url, 'http://www.pypy.org')\n\n# Get the results back\nx = a.result()\ny = b.result()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f8b\u5b50\u4e2d\u8fd4\u56de\u7684handle\u5bf9\u8c61\u4f1a\u5e2e\u4f60\u5904\u7406\u6240\u6709\u7684\u963b\u585e\u4e0e\u534f\u4f5c\uff0c\u7136\u540e\u4ece\u5de5\u4f5c\u7ebf\u7a0b\u4e2d\u8fd4\u56de\u6570\u636e\u7ed9\u4f60\u3002\n\u7279\u522b\u7684\uff0ca.result() \u64cd\u4f5c\u4f1a\u963b\u585e\u8fdb\u7a0b\u76f4\u5230\u5bf9\u5e94\u7684\u51fd\u6570\u6267\u884c\u5b8c\u6210\u5e76\u8fd4\u56de\u4e00\u4e2a\u7ed3\u679c\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u901a\u5e38\u6765\u8bb2\uff0c\u4f60\u5e94\u8be5\u907f\u514d\u7f16\u5199\u7ebf\u7a0b\u6570\u91cf\u53ef\u4ee5\u65e0\u9650\u5236\u589e\u957f\u7684\u7a0b\u5e8f\u3002\u4f8b\u5982\uff0c\u770b\u770b\u4e0b\u9762\u8fd9\u4e2a\u670d\u52a1\u5668\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from threading import Thread\nfrom socket import socket, AF_INET, SOCK_STREAM\n\ndef echo_client(sock, client_addr):\n    '''\n    Handle a client connection\n    '''\n    print('Got connection from', client_addr)\n    while True:\n        msg = sock.recv(65536)\n        if not msg:\n            break\n        sock.sendall(msg)\n    print('Client closed connection')\n    sock.close()\n\ndef echo_server(addr, nworkers):\n    # Run the server\n    sock = socket(AF_INET, SOCK_STREAM)\n    sock.bind(addr)\n    sock.listen(5)\n    while True:\n        client_sock, client_addr = sock.accept()\n        t = Thread(target=echo_client, args=(client_sock, client_addr))\n        t.daemon = True\n        t.start()\n\necho_server(('',15000))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5c3d\u7ba1\u8fd9\u4e2a\u4e5f\u53ef\u4ee5\u5de5\u4f5c\uff0c\n\u4f46\u662f\u5b83\u4e0d\u80fd\u62b5\u5fa1\u6709\u4eba\u8bd5\u56fe\u901a\u8fc7\u521b\u5efa\u5927\u91cf\u7ebf\u7a0b\u8ba9\u4f60\u670d\u52a1\u5668\u8d44\u6e90\u67af\u7aed\u800c\u5d29\u6e83\u7684\u653b\u51fb\u884c\u4e3a\u3002\n\u901a\u8fc7\u4f7f\u7528\u9884\u5148\u521d\u59cb\u5316\u7684\u7ebf\u7a0b\u6c60\uff0c\u4f60\u53ef\u4ee5\u8bbe\u7f6e\u540c\u65f6\u8fd0\u884c\u7ebf\u7a0b\u7684\u4e0a\u9650\u6570\u91cf\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u53ef\u80fd\u4f1a\u5173\u5fc3\u521b\u5efa\u5927\u91cf\u7ebf\u7a0b\u4f1a\u6709\u4ec0\u4e48\u540e\u679c\u3002\n\u73b0\u4ee3\u64cd\u4f5c\u7cfb\u7edf\u53ef\u4ee5\u5f88\u8f7b\u677e\u7684\u521b\u5efa\u51e0\u5343\u4e2a\u7ebf\u7a0b\u7684\u7ebf\u7a0b\u6c60\u3002\n\u751a\u81f3\uff0c\u540c\u65f6\u51e0\u5343\u4e2a\u7ebf\u7a0b\u7b49\u5f85\u5de5\u4f5c\u5e76\u4e0d\u4f1a\u5bf9\u5176\u4ed6\u4ee3\u7801\u4ea7\u751f\u6027\u80fd\u5f71\u54cd\u3002\n\u5f53\u7136\u4e86\uff0c\u5982\u679c\u6240\u6709\u7ebf\u7a0b\u540c\u65f6\u88ab\u5524\u9192\u5e76\u7acb\u5373\u5728CPU\u4e0a\u6267\u884c\uff0c\u90a3\u5c31\u4e0d\u540c\u4e86\u2014\u2014\u7279\u522b\u662f\u6709\u4e86\u5168\u5c40\u89e3\u91ca\u5668\u9501GIL\u3002\n\u901a\u5e38\uff0c\u4f60\u5e94\u8be5\u53ea\u5728I/O\u5904\u7406\u76f8\u5173\u4ee3\u7801\u4e2d\u4f7f\u7528\u7ebf\u7a0b\u6c60\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u521b\u5efa\u5927\u7684\u7ebf\u7a0b\u6c60\u7684\u4e00\u4e2a\u53ef\u80fd\u9700\u8981\u5173\u6ce8\u7684\u95ee\u9898\u662f\u5185\u5b58\u7684\u4f7f\u7528\u3002\n\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u5728OS X\u7cfb\u7edf\u4e0a\u9762\u521b\u5efa2000\u4e2a\u7ebf\u7a0b\uff0c\u7cfb\u7edf\u663e\u793aPython\u8fdb\u7a0b\u4f7f\u7528\u4e86\u8d85\u8fc79GB\u7684\u865a\u62df\u5185\u5b58\u3002\n\u4e0d\u8fc7\uff0c\u8fd9\u4e2a\u8ba1\u7b97\u901a\u5e38\u662f\u6709\u8bef\u5dee\u7684\u3002\u5f53\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u65f6\uff0c\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u9884\u7559\u4e00\u4e2a\u865a\u62df\u5185\u5b58\u533a\u57df\u6765\n\u653e\u7f6e\u7ebf\u7a0b\u7684\u6267\u884c\u6808\uff08\u901a\u5e38\u662f8MB\u5927\u5c0f\uff09\u3002\u4f46\u662f\u8fd9\u4e2a\u5185\u5b58\u53ea\u6709\u4e00\u5c0f\u7247\u6bb5\u88ab\u5b9e\u9645\u6620\u5c04\u5230\u771f\u5b9e\u5185\u5b58\u4e2d\u3002\n\u56e0\u6b64\uff0cPython\u8fdb\u7a0b\u4f7f\u7528\u5230\u7684\u771f\u5b9e\u5185\u5b58\u5176\u5b9e\u5f88\u5c0f\n\uff08\u6bd4\u5982\uff0c\u5bf9\u4e8e2000\u4e2a\u7ebf\u7a0b\u6765\u8bb2\uff0c\u53ea\u4f7f\u7528\u5230\u4e8670MB\u7684\u771f\u5b9e\u5185\u5b58\uff0c\u800c\u4e0d\u662f9GB\uff09\u3002\n\u5982\u679c\u4f60\u62c5\u5fc3\u865a\u62df\u5185\u5b58\u5927\u5c0f\uff0c\u53ef\u4ee5\u4f7f\u7528 threading.stack_size() \u51fd\u6570\u6765\u964d\u4f4e\u5b83\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import threading\nthreading.stack_size(65536)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u52a0\u4e0a\u8fd9\u6761\u8bed\u53e5\u5e76\u518d\u6b21\u8fd0\u884c\u524d\u9762\u7684\u521b\u5efa2000\u4e2a\u7ebf\u7a0b\u8bd5\u9a8c\uff0c\n\u4f60\u4f1a\u53d1\u73b0Python\u8fdb\u7a0b\u53ea\u4f7f\u7528\u5230\u4e86\u5927\u6982210MB\u7684\u865a\u62df\u5185\u5b58\uff0c\u800c\u771f\u5b9e\u5185\u5b58\u4f7f\u7528\u91cf\u6ca1\u6709\u53d8\u3002\n\u6ce8\u610f\u7ebf\u7a0b\u6808\u5927\u5c0f\u5fc5\u987b\u81f3\u5c11\u4e3a32768\u5b57\u8282\uff0c\u901a\u5e38\u662f\u7cfb\u7edf\u5185\u5b58\u9875\u5927\u5c0f\uff084096\u30018192\u7b49\uff09\u7684\u6574\u6570\u500d\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}